(function() {
    function init() {
        bindEvent();
        scroll();
        initWx();
        setTimeout(() => {
            reqPosList();
        }, 1000);
    }
    function bindEvent() {
        $('#submitBtn').on('click', function() {
            var receiver = $('#receiver').val().trim();
            var mobile = $('#mobile').val().trim();
            var address = $('#address').val().trim();
            var userType = parseInt($('#userType').val(), 0);

            if (!receiver) {
                alert('请先填写收货人');
                return;
            }
            if (!mobile) {
                alert('请先填写联系方式');
                return;
            }
            if (!address) {
                alert('请先填写收货地址');
                return;
            }
            $.ajax({
                type: 'POST', // 
                url: 'http://api.pos.wezones.net/pos/record',
                data: JSON.stringify({
                    username: receiver,
                    contact: mobile,
                    addr: address,
                    userType: userType,
                }),
                dataType: 'json',
                contentType: "application/json",
                success: function(data) {
                    // console.log(data);
                    if (!data.code) {
                        alert('提交成功');
                        $('#formWrap').remove();
                        return;
                    }
                    alert('服务器繁忙，请稍候再试');
                },
                error: function(err) {
                    // console.log('-->', err);
                    alert('服务器繁忙，请稍候再试');
                }
            })
        });
        $('#quickOrderBtn').on('click', function() {
            $('html, body').animate({
                scrollTop: $('.title-wrap').offset().top
            }, 500);
        });
        $('#wechat').on('click', function() {
            $('#wechatModal').show();
            $('body').css('overflow', 'hidden');
        });
        $('#linkAbout').on('click', function() {
            $('#aboutModal').show();
            $('body').css('overflow', 'hidden');
        });
        $('#linkContact').on('click', function() {
            $('#modalContact').show();
            $('body').css('overflow', 'hidden');
        });
        $('.modal .bg').on('click', function() {
            $('body').css('overflow', 'auto');
            $('.modal').hide();
        });
    }
    // var initSlider = function (containerId) {
    //     var options = { $AutoPlay: 1 };
    //     var jssor_slider1 = new $JssorSlider$(containerId, options);
    // };

    function scroll() {
        $('#comments').liMarquee({
            direction: 'up'
        });
        $('.pos-list .items').liMarquee({
            direction: 'left'
        });
        $('#banner').swipeSlide({
            // autoSwipe : false,            
        });
        var winWidth = $(window).width();
        var height = winWidth / 450 * 425;
        $('#banner').height(height);
        $('#banner ul').width(winWidth * 2);
        // initSlider("banner");
        // var imgsWrap = $('#banner .imgs');
        // var left = 0;
        // setInterval(function() {
        //     left += 5;
        //     if (left > winWidth) {
        //         $('img', imgsWrap).after()
        //         left = 0;
        //     }
        //     imgsWrap.css('margin-left', (-left) + 'px');
        // }, 20);
        // $('#banner .imgs').liMarquee({
        //     direction: 'right'
        // });
        // var commentDom = $('#comments');
        // var max = 423;
        // var cur = 0;
        // var interval = 30;
        // var gap = interval * 1.0 / 6000 * max;
        // setInterval(function() {
        //     commentDom.css('margin-top', -cur + 'px');
        //     cur += gap;
        //     if (cur >= max) {
        //         cur = 0;
        //     }
        //     // console.log(cur);
        // }, interval);
    }

    function initWx() {
        var url = location.href;
        var index = url.indexOf('#');
        if (index > 0) {
            url = url.substr(0, index);
        }
        $.get('http://api.pos.wezones.net/wechat/js_config'
            + '?url=' + escape(url),
            function(rsp) {
                // console.log(rsp);
                if (rsp.code === 0) {
                    var data = rsp.data;
                    if (!data){
                        return;
                    }
                    wx.config({
                        debug: false,
                        appId: data.appId, // 必填，公众号的唯一标识
                        timestamp: data.timestamp,
                        nonceStr: data.nonceStr,
                        signature: data.signature,
                        jsApiList: [
                            'onMenuShareTimeline', 'onMenuShareAppMessage'
                        ]
                    });
                }
            });
        wx.ready(function(){
            wx.onMenuShareTimeline({
                title: '微POS机免费领取，费率低至0.5%，全天24小时秒到，轻松调配现金',
                link: 'http://h5.wepos.top/',
                imgUrl: 'http://ot6h89xw6.bkt.clouddn.com/assets/imgs/wechat-share.jpg', // 分享图标
                success: function () { 
                    // 用户确认分享后执行的回调函数
                },
                cancel: function () { 
                    // 用户取消分享后执行的回调函数
                }
            });
            wx.onMenuShareAppMessage({
                title: '微POS机免费领啦！',
                desc: '费率低至0.5%，全天24小时秒到，轻松调配现金，银联安全认证', // 分享描述
                link: 'http://h5.wepos.top/', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: 'http://ot6h89xw6.bkt.clouddn.com/assets/imgs/wechat-share.jpg', // 分享图标
                success: function () { 
                    // 用户确认分享后执行的回调函数
                },
                cancel: function () { 
                    // 用户取消分享后执行的回调函数
                }
            });
        });
        // wx.error(function(res){

        // });        
    }

    function reqPosList() {
        $.ajax({
            type: 'POST', // 
            url: 'http://api.pos.wezones.net/pos/pos_list',
            contentType: "application/json",
            success: function(rsp) {
                // console.log(data);赵[138****2524] 3小时前商家版微POS机
                if (rsp.code === 0) {
                    var msg = '';
                    rsp.data.forEach((item) => {
                        var now = new Date().getTime();
                        var then = new Date(item.minutes).getTime();
                        var duration = parseInt((now - then) / 1000 / 3600);
                        if (duration > 24) {
                            duration = parseInt(duration/24) + '天前';
                        } else if (duration <= 1) {
                            duration = '1小时内';
                        } else if (duration > 1) {
                            duration = duration + '小时前';
                        }
                        var type = item.userType;
                        var userType = '';
                        switch (type) {
                        case 1:
                            userType = '个人版';
                            break;
                        case 2:
                            userType = '商户版';
                            break;
                        case 3:
                            userType = '代理版';
                            break;
                        }
                        msg += '<li class="item">' + item.username + '['
                            + item.contact + ']' + duration + '免费领取'
                            + userType + '微POS机</li>';
                    });
                    if (msg) {
                        $('.pos-list .items .str_move').html(msg);
                    }
                }
            },
            error: function(err) {
                // alert('服务器繁忙，请稍候再试');
            }
        })
    }

    init();
})();
